package com.mall.admin.service;

import cn.hutool.core.bean.BeanUtil;
import com.mall.domain.SmsFlashPromotionSession;
import com.mall.interface1.api.SmsFlashPromotionSessionApi;
import com.mall.vo.FlashSessionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SmsFlashPromotionSessionService {

    @DubboReference
    private SmsFlashPromotionSessionApi smsFlashPromotionSessionApi;

    //添加场次
    public void create(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionApi.create(smsFlashPromotionSession);
    }

    //删除场次
    public void delete(Long id) {
        smsFlashPromotionSessionApi.deleteById(id);
    }

    //获取全部场次
    public List<SmsFlashPromotionSession> list() {
        return smsFlashPromotionSessionApi.list();
    }

    //获取全部可选场次及其数量
    public List<FlashSessionVo> selectList(Long flashPromotionId) {
        List<FlashSessionVo> voList = new ArrayList<>();
        //获取所有的FlashSession数据
        List<SmsFlashPromotionSession> sfpsList = smsFlashPromotionSessionApi.selectSession();
        for (SmsFlashPromotionSession item : sfpsList) {
            FlashSessionVo vo = new FlashSessionVo();
            BeanUtil.copyProperties(item,vo);
            //根据条件获得活动对应产品数量
            Integer count = smsFlashPromotionSessionApi.selectCount(flashPromotionId,item.getId());
            vo.setProductCount(count);
            voList.add(vo);
        }
        return voList;
    }

    //修改启用状态
    public void updateStatus(Long id, Integer status) {
        //根据id获取活动
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionApi.findById(id);
        smsFlashPromotionSession.setStatus(status);
        //修改状态
        smsFlashPromotionSessionApi.updateStatus(smsFlashPromotionSession);
    }

    //修改场次
    public void update(Long id, SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSession.setId(id);
        smsFlashPromotionSessionApi.update(smsFlashPromotionSession);
    }

    //获取场次详情
    public SmsFlashPromotionSession find(Long id) {
        return smsFlashPromotionSessionApi.findById(id);
    }
}
